//
//  DFBaseLineCell.m
//  DFTimelineView
//
//  Created by Allen Zhong on 15/9/27.
//  Copyright (c) 2015年 Datafans, Inc. All rights reserved.
//





#define UserNickFont [UIFont systemFontOfSize:15]
#define TitleLabelFont [UIFont systemFontOfSize:13]

#define LocationLabelFont [UIFont systemFontOfSize:10]

#define TimeLabelFont [UIFont systemFontOfSize:12]

//#define UserNickLabelHeight 15
#define UserNickMaxWidth 150

#define LocationLabelHeight 15

#define TimeLabelHeight 15

#define UserNickLineHeight 1.0f


#define LikeLabelFont [UIFont systemFontOfSize:14]

#define LikeLabelLineHeight 1.1f

#define LikeCommentTimeSpace 3
#define ToolbarWidth 150
#define ToolbarHeight 30
#import "DFBaseLineCell.h"
#import "MLLabel+Size.h"
#import "DFLikeCommentView.h"
#import "DFToolUtil.h"
#import "DFLineLikeItem.h"
#import "DFGridImageView.h"

#import "DFTextImageLineItem.h"

#import "MLLabel+Size.h"

#import "DFGridImageView.h"


#import "NSString+MLExpression.h"

#import "DFFaceManager.h"

#import "HisHomePageViewController.h"
#import "TribeDetailViewController.h"
#import "YXCustomActionSheet.h"
#import "TopicleWebViewController.h"

#import "DyShare.h"
#import "YXCustomActionSheet.h"
#import "DFLikeCommentToolbar.h"
#define TextImageCell @"timeline_cell_text_image"


#define TextFont [UIFont systemFontOfSize:16]

#define TextLineHeight 1.2f

#define TextImageSpace 10
#define BigImgHeight 180
#define SmallImgHeight 50
#define TextFieldSpace 10
#define buttonSpace 10
#define TextFieldHeight 0
#define CommentShowMaxNum 5
#define GridMaxWidth [UIScreen mainScreen].bounds.size.width - 2*Margin
#define GridMaxHeight BigImgHeight+Padding+SmallImgHeight
@interface DFBaseLineCell()<DFLikeCommentViewDelegate,UITextFieldDelegate,YXCustomActionSheetDelegate,DFBigImgDelegate,DFLikeCommentToolbarDelegate>


@property (nonatomic, strong) DFBaseLineItem *item;

@property (nonatomic, strong) UIImageView *userAvatarView;
@property (nonatomic, strong) UIButton *userAvatarButton;

@property (nonatomic, strong) UILabel *userNickLabel;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *btnTitleLabel;

@property (nonatomic, strong) UILabel *locationLabel;

@property (nonatomic, strong) UILabel *timeLabel;

@property (nonatomic, strong) UIButton *likeCmtButton;

@property (nonatomic, strong) UIButton *moreButton;

@property (nonatomic, strong) UIButton *shareButton;
@property (nonatomic, strong) UIButton *likeButton;
@property (nonatomic, strong) UIButton *commentButton;

@property (nonatomic, strong) DFLikeCommentView *likeCommentView;

@property (nonatomic, strong) UIButton *showLikeCountBtn;

@property (nonatomic, strong) UIButton *guanzhuBtn;

@property (nonatomic, strong) UIView *bgView;

@property (strong, nonatomic) MLLinkLabel *textContentLabel;

@property (strong, nonatomic) DFGridImageView *gridImageView;

@property (strong, nonatomic) UIButton *showMoreBtn;
@property (strong, nonatomic) UITextField *textField;

@property (nonatomic, strong) DFLikeCommentToolbar *likeCommentToolbar;

@property (nonatomic, assign) BOOL isLikeCommentToolbarShow;

@property (strong, nonatomic)UIImage *shareImg;
@property (assign, nonatomic) BOOL isManger;

@end



@implementation DFBaseLineCell


#pragma mark - Lifecycle

- (void)setFrame:(CGRect)frame
{
    // 更改x、宽度
    CGFloat kTableTopBorderWidth = 10;
    CGFloat kTableViewCellMargin = 10;
    //    frame.origin.x = kTableBorderWidth;
    //    frame.size.width -= kTableBorderWidth * 2;
    
    // 更改顶部间距、每个cell之间的间距
//    frame.origin.y += kTableTopBorderWidth;
    frame.size.height -= kTableViewCellMargin;
    [super setFrame:frame];
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _isLikeCommentToolbarShow = NO;
        [self initBaseCellWithIsManger:NO];
    }
    return self;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier isManger:(BOOL)isManger
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _isLikeCommentToolbarShow = NO;
        [self initBaseCellWithIsManger:isManger];
    }
    return self;
}
//n1 n12
-(void) initBaseCellWithIsManger:(BOOL)isManger{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    _isManger = isManger;
    CGFloat x = 0.0, y, width, height;
    //用户头像
    if (_userAvatarView == nil ) {
        
        x = Margin;
        y = Margin;
        width = UserAvatarSize;
        height = width;
        _userAvatarView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        _userAvatarView.layer.cornerRadius = 20;
        _userAvatarView.layer.masksToBounds = YES;
        _userAvatarView.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:_userAvatarView];
        
        _userAvatarButton = [[UIButton alloc] initWithFrame:_userAvatarView.frame];
        [_userAvatarButton addTarget:self action:@selector(onClickUserAvatar:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_userAvatarButton];
    }
    //用户名
    if (_userNickLabel == nil) {
        _userNickLabel =[[MLLinkLabel alloc] initWithFrame:CGRectZero];
        _userNickLabel.textColor = [UIColor blackColor];
        _userNickLabel.font = UserNickFont;
        [self addSubview:_userNickLabel];
    }
    //标题
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.textColor = QINGColor;
        _titleLabel.font = TitleLabelFont;
        [self addSubview:_titleLabel];
    }
    //关注按钮
    if (_guanzhuBtn == nil) {
        _guanzhuBtn = [UIButton buttonWithType:0];
        [_guanzhuBtn setBackgroundColor:QINGColor];
        _guanzhuBtn.layer.cornerRadius = 5;
        _guanzhuBtn.layer.masksToBounds = YES;
        [_guanzhuBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_guanzhuBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_guanzhuBtn setTitle:@"+关注" forState:UIControlStateNormal];
        [_guanzhuBtn setTitle:@"已关注" forState:UIControlStateSelected];
        [_guanzhuBtn setFont:[UIFont systemFontOfSize:15]];
        _guanzhuBtn.frame = CGRectMake(SCREEN_WIDTH-20-60, 20, 60, 27);
        [self addSubview:_guanzhuBtn];
        _guanzhuBtn.hidden = YES;
        [_guanzhuBtn addTarget:self action:@selector(guanzhuAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    //图片+内容文字详情 布局层
    if (_bodyView == nil) {
        x = 15;
        y = CGRectGetMaxY(_userAvatarView.frame)+Padding;
        width = BodyMaxWidth;
        height = 1;
        _bodyView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        _bodyView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundClickAction)];
        tap.numberOfTapsRequired = 1;
        [_bodyView addGestureRecognizer:tap];
        [self addSubview:_bodyView];
    }
    
    //内容文字详情
    if (_textContentLabel == nil) {
        _textContentLabel.userInteractionEnabled = NO;
        _textContentLabel =[[MLLinkLabel alloc] initWithFrame:CGRectZero];
        _textContentLabel.font = TextFont;
        _textContentLabel.numberOfLines = 0;
        _textContentLabel.adjustsFontSizeToFitWidth = NO;
        _textContentLabel.textInsets = UIEdgeInsetsZero;
        
        _textContentLabel.dataDetectorTypes = MLDataDetectorTypeAll;
        _textContentLabel.allowLineBreakInsideLinks = NO;
        _textContentLabel.linkTextAttributes = nil;
        _textContentLabel.activeLinkTextAttributes = nil;
        _textContentLabel.lineHeightMultiple = TextLineHeight;
        _textContentLabel.linkTextAttributes = @{NSForegroundColorAttributeName: HighLightTextColor};
        
        [_textContentLabel setDidClickLinkBlock:^(MLLink *link, NSString *linkText, MLLinkLabel *label) {
            NSString *tips = [NSString stringWithFormat:@"Click\nlinkType:%ld\nlinkText:%@\nlinkValue:%@",(unsigned long)link.linkType,linkText,link.linkValue];
            NSLog(@"%@", tips);
        }];
        [self.bodyView addSubview:_textContentLabel];
    }
    
    //整个图片层
    if (_gridImageView == nil) {
        
        CGFloat x, y , width, height;
        
        x = 0;
        y = 0;
        width = GridMaxWidth;
        height = GridMaxHeight;
        
        _gridImageView = [[DFGridImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        _gridImageView.userInteractionEnabled = YES;
        [self.bodyView addSubview:_gridImageView];
    }
    
    //时间label
    if (_timeLabel == nil) {
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _timeLabel.textColor = [UIColor lightGrayColor];
        _timeLabel.font = TimeLabelFont;
        _timeLabel.hidden = NO;
        _timeLabel.textAlignment = NSTextAlignmentRight;
        [self addSubview:_timeLabel];
    }
    
    //分享和评论
    if (_bgView == nil) {
        _bgView = [[UIView alloc]initWithFrame:CGRectZero];
        [self addSubview:_bgView];
    }
    
    if (_isManger == YES) {
        
    }
    
    //喜欢和评论层
    if (_likeCommentView == nil) {
        _likeCommentView = [[DFLikeCommentView alloc] initWithFrame:CGRectMake(0, 0, BodyMaxWidth, 1)];
        _likeCommentView.delegate = self;
        
        [self addSubview:_likeCommentView];
    }
    
    
    if (_likeCmtButton == nil) {
        _likeCmtButton = [[UIButton alloc] initWithFrame:CGRectZero];
        [_likeCmtButton setImage:[UIImage imageNamed:@"AlbumOperateMore"] forState:UIControlStateNormal];
        [_likeCmtButton setImage:[UIImage imageNamed:@"AlbumOperateMoreHL"] forState:UIControlStateHighlighted];
        [_likeCmtButton addTarget:self action:@selector(onClickLikeCommentBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_likeCmtButton];
    }
    
    if (_likeCommentToolbar == nil) {
        y = 0;
        x = 0;
        width = ToolbarWidth;
        height = ToolbarHeight;
        _likeCommentToolbar = [[DFLikeCommentToolbar alloc] initWithFrame:CGRectMake(x, y, width, height)];
        _likeCommentToolbar.delegate = self;
        _likeCommentToolbar.hidden = YES;
        [self.contentView addSubview:_likeCommentToolbar];
    }
    if (_showMoreBtn == nil) {
        _showMoreBtn = [UIButton buttonWithType:0];
        [self addSubview:_showMoreBtn];
    }
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(HiddenCommentBar) name:@"HiddenCommentBar"object:nil];
}

- (void)HiddenCommentBar{
    
    [self hideLikeCommentToolbar];
}

-(void) onClickLikeCommentBtn:(id)sender
{
    _isLikeCommentToolbarShow = !_isLikeCommentToolbarShow;
    _likeCommentToolbar.hidden = !_isLikeCommentToolbarShow;
    //动画效果
    CGFloat x = CGRectGetMinX(_likeCmtButton.frame)-ToolbarWidth-10;
    CGFloat y = CGRectGetMinY(_likeCmtButton.frame);
    CGFloat width = ToolbarWidth;
    CGFloat height = ToolbarHeight;
    _likeCommentToolbar.layer.anchorPoint =CGPointMake(0.5,0.5);//设置锚点
     _likeCommentToolbar.frame = CGRectMake(x + width / 2, y, width, height);
    if (_isLikeCommentToolbarShow) {
        CABasicAnimation *theAnimation;
        theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
        theAnimation.duration=0.2;
        theAnimation.removedOnCompletion = YES;
      
        theAnimation.fromValue = [NSNumber numberWithFloat:0];
        theAnimation.toValue = [NSNumber numberWithFloat:1];
        _likeCommentToolbar.layer.anchorPoint =CGPointMake(1.0,0.5);
        [_likeCommentToolbar.layer addAnimation:theAnimation forKey:@"animateTransform"];
    }
}



-(void)hideLikeCommentToolbar
{
    if (_isLikeCommentToolbarShow == NO) {
        return;
    }
    _isLikeCommentToolbarShow = NO;
    _likeCommentToolbar.hidden = YES;
}

-(void)onLike
{
    
    [self hideLikeCommentToolbar];
    
    if (_delegate != nil && [_delegate respondsToSelector:@selector(onLike:likeItem:type:)]) {
        [_delegate onLike:self.item.content_id likeItem:self.item.likes type:0];
    }
}

- (void)backgroundClickAction {
    
    [self onClickBigImg];
}

//n13
-(void)updateWithItem:(DFTextImageLineItem *)item{
   
    self.item = item;
//    NSString *headUrl = [NSString stringWithFormat:@"%@?imageView2/0/w/%d/h/%d",item.head_url,UserAvatarSize,UserAvatarSize];
    [_userAvatarView sd_setImageWithURL:[NSURL URLWithString:item.head_url] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    
    CGFloat x, y, width, height;
    x = CGRectGetMaxX(_userAvatarView.frame) + Margin;
    y = CGRectGetMinY(_userAvatarView.frame);
    width = 100;
    height = 20;
    
    _userNickLabel.frame = CGRectMake(x, y, width, height);
    _userNickLabel.text = item.nick_name;
    y = CGRectGetMaxY(_userAvatarView.frame)-15;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 45, 15)];
    label.text = @"发布于:";
    label.textColor = [UIColor grayColor];
    label.font = [UIFont systemFontOfSize:13];
    [self addSubview:label];
    x = CGRectGetMaxX(label.frame) + Padding;
    width = [UIScreen mainScreen].bounds.size.width - x - Margin;
    _titleLabel.frame = CGRectMake(x, y, width, height);
    NSString *str = item.tribe_name;
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:13],};
    CGSize size = [str boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;;
    
    [_titleLabel setFrame:CGRectMake(x, y, size.width, size.height)];
    _titleLabel.text = str;
    _titleLabel.userInteractionEnabled = YES;
    if (_btnTitleLabel == nil ) {
        _btnTitleLabel = [UIButton buttonWithType:0];
        _btnTitleLabel.frame = CGRectMake(0, 0, _titleLabel.frame.size.width, _titleLabel.frame.size.height);
        [_btnTitleLabel setBackgroundColor:[UIColor whiteColor]];
        [_btnTitleLabel addTarget:self action:@selector(titleLabelAction:) forControlEvents:UIControlEventTouchUpInside];
        [_titleLabel addSubview:_btnTitleLabel];
    }
    y = CGRectGetMaxY(_titleLabel.frame);
    CGSize textSize = [MLLinkLabel getViewSize:item.attrText maxWidth:BodyMaxWidth font:TextFont lineHeight:TextLineHeight lines:0];
    _textContentLabel.attributedText = item.attrText;
    [_textContentLabel sizeToFit];
    
    _textContentLabel.frame = CGRectMake(0, 0, BodyMaxWidth, textSize.height);
    
    CGFloat gridHeight = [DFGridImageView getHeight:item.srcImages maxWidth:GridMaxWidth oneImageWidth:item.width oneImageHeight:item.height];
    
    x = _gridImageView.frame.origin.x;
    y = CGRectGetMaxY(_textContentLabel.frame)+TextImageSpace;
    width = _gridImageView.frame.size.width;
    height = gridHeight;
    _gridImageView.frame = CGRectMake(x, y, width, height);
    _gridImageView.delegate = self;
    [_gridImageView updateWithSrcImages:item.srcImages oneImageWidth:item.width oneImageHeight:item.height];
    x = _bodyView.frame.origin.x;
    y = _bodyView.frame.origin.y;
    width = _bodyView.frame.size.width;
    height = textSize.height+gridHeight+TextImageSpace+Padding;
    _bodyView.frame = CGRectMake(x, y, width, height);
//    _bodyView.backgroundColor = [UIColor redColor];
    //时间
    CGFloat newHeight =  _userAvatarView.frame.origin.y+_userAvatarView.frame.size.height+TextImageSpace+height;
    y = CGRectGetMaxY(_bodyView.frame) + Padding;
    width = 200;
    height = TimeLabelHeight;
    _timeLabel.frame = CGRectMake(SCREEN_WIDTH-200-15, 15, 200, 20);
    _timeLabel.text = [DFToolUtil preettyTime:self.item.publish_tm];
    _likeCmtButton.frame = CGRectMake(SCREEN_WIDTH-15-40, newHeight, 40, 30);
//    _likeCmtButton.backgroundColor = [UIColor redColor];
    _likeCommentToolbar.frame = CGRectMake(SCREEN_WIDTH-15-40-ToolbarWidth-10, newHeight, ToolbarWidth, 30);
    if (_isManger == YES) {
        x = CGRectGetMaxX(_bodyView.frame)  - 150;
        
        _bgView.frame = CGRectMake(x, newHeight+50, 150, 30);
    }else{
        
        x = CGRectGetMaxX(_bodyView.frame)  - 100;
        
        _bgView.frame = CGRectMake(x, newHeight+50, 100, 30);
    }
    
    //点赞和评论
    x = CGRectGetMinX(_bodyView.frame);
    y = CGRectGetMaxY(_likeCmtButton.frame)+LikeCommentTimeSpace;
    width = _likeCommentView.frame.size.width;
    height = [DFLikeCommentView getHeight:self.item maxWidth:BodyMaxWidth];
    _likeCommentView.frame = CGRectMake(x, y, width, height);
    [_likeCommentView updateWithItem:self.item];
    
    y = CGRectGetMaxY(_likeCommentView.frame) +buttonSpace;
    if (self.item.comments.count>CommentShowMaxNum) {
        _showMoreBtn.frame = CGRectMake(x, y, 130, 30);
        _showMoreBtn.hidden = NO;
        NSString *str = [NSString stringWithFormat:@"查看全部%ld条评论",self.item.comments.count];
        [_showMoreBtn setTitle:str forState:UIControlStateNormal];
        [_showMoreBtn addTarget:self action:@selector(showMoreCommentBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_showMoreBtn setFont:[UIFont systemFontOfSize:14]];
        y = CGRectGetMaxY(_showMoreBtn.frame)+TextFieldSpace;
        [_showMoreBtn setTitleColor:[UIColor colorWithHexString:@"0EA1B6"] forState:UIControlStateNormal];
       
    }else{
        _showMoreBtn.hidden  = YES;
    }

    //点赞评论按钮
    width = 25;
    height = 25;
    x = CGRectGetMaxX(_bodyView.frame) - width;
    
    [self downloadShareImage];
    
}
- (void)onClickBigImg{
    UIViewController *vc =  [self getCurrentVC];
    TopicleWebViewController *topic = [[TopicleWebViewController alloc]initWithNibName:@"TopicleWebViewController" bundle:nil];
    topic.articleContent = self.item.content_id;
    topic.articleUserId = self.item.user_id;
    topic.articleTribeId = self.item.tribe_id;
    topic.shareurl = self.item.shareUrl;
    topic.shareimgurl = self.item.head_url;
    topic.sharetitle = self.item.title;
    topic.sharedesc = self.item.content_desc;
    [vc.navigationController pushViewController:topic animated:YES];
}
- (void)titleLabelAction:(UIButton *)button{
    NSString *tribeId = self.item.tribe_id;
    UIViewController *vc = [self getCurrentVC];
    TribeDetailViewController *detail = [[TribeDetailViewController alloc]initWithNibName:@"TribeDetailViewController" bundle:nil];
    [vc.navigationController pushViewController:detail animated:YES];
    detail.tribeID = tribeId;
    
}

-(CGFloat)getCellHeight:(DFTextImageLineItem *)item{
    
    if (item.attrText == nil) {
        item.attrText  = [item.text expressionAttributedStringWithExpression:[[DFFaceManager sharedInstance] sharedMLExpression]];
    }
    
    CGSize textSize = [MLLinkLabel getViewSize:item.attrText maxWidth:BodyMaxWidth font:TextFont lineHeight:TextLineHeight lines:0];
    
    //基本
    CGFloat height = Margin + UserAvatarSize;
    
    //位置
    if (item.location != nil && ![item.location isEqualToString:@""]) {
        height+=LocationLabelHeight+Padding;
    }
    
    //时间
    height+= TimeLabelHeight + Padding;
    
    //图片高度
    CGFloat gridHeight = [DFGridImageView getHeight:item.srcImages maxWidth:GridMaxWidth oneImageWidth:item.width oneImageHeight:item.height];
    
    //点赞和评论
    height+=[DFLikeCommentView getHeight:item maxWidth:BodyMaxWidth]+LikeCommentTimeSpace;
    
    height = height+textSize.height + gridHeight+TextImageSpace;
    
    if (item.comments.count>CommentShowMaxNum) {
        
        height = height + TextFieldSpace + TextFieldHeight + 20+buttonSpace+TextFieldSpace;
    }else{
        //文本框
        height = height + TextFieldSpace + TextFieldHeight;
    }
    
    return height ;
}
#pragma mark - Method

-(CGFloat)getReuseableCellHeight:(DFBaseLineItem *)item{
    if (item.cellHeight != 0) {
        NSLog(@"重用高度 %f", item.cellHeight);
        return item.cellHeight;
    }
    CGFloat height = [self getCellHeight:item];
    item.cellHeight = height;
    NSLog(@"计算高度 %f", item.cellHeight);
    
    return height;
}

-(DFBaseLineCell *)getCell:(UITableView *)tableView{
    return nil;
}


//-(UINavigationController *)getController{
//    UITableView *tableView = (UITableView *)self.superview.superview;
//    UIViewController *controller = (UIViewController *)tableView.dataSource;
//    return controller.navigationController;
//}


-(void) onClickUserAvatar:(id)sender{
    
    UIViewController *vc = [self getCurrentVC];
    HisHomePageViewController *homepage = [[HisHomePageViewController alloc]init];
    homepage.userid = self.item.user_id;
    [vc.navigationController pushViewController:homepage animated:YES];
}


#pragma mark - DFLikeCommentToolbarDelegate

- (void)showMoreCommentBtnAction:(UIButton *)button{
    
    [self onClickBigImg];
}

- (void) onClickLikeType:(BOOL)type
{
    if (_delegate != nil && [_delegate respondsToSelector:@selector(onLike:likeItem:type:)]) {
        [_delegate onLike:self.item.content_id likeItem:self.item.likes type:type];
    }
}
//- (void)onMore
//{
//    NSLog(@"onMore");
//    UIAlertController *alertController = [[UIAlertController alloc]init];
//    
//    // Create the actions.
//    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
//        NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");
//    }];
//    
//    UIAlertAction *shareAction = [UIAlertAction actionWithTitle:@"分享部落" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        NSLog(@"分享部落");
//    }];
//    
//    
//    UIAlertAction *joinAction = [UIAlertAction actionWithTitle:@"加入部落" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        NSLog(@"加入部落");
//    }];
//    
//    UIAlertAction *reportAction = [UIAlertAction actionWithTitle:@"举报部落" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        NSLog(@"举报部落");
//    }];
//    
//    [alertController addAction:cancelAction];
//    [alertController addAction:shareAction];
//    [alertController addAction:joinAction];
//    [alertController addAction:reportAction];
//    UIViewController *vc = [self getCurrentVC];
//    [vc presentViewController:alertController animated:YES completion:^{
//        NSLog(@"presented");
//    }];
//}


-(void)onComment
{
    [self hideLikeCommentToolbar];
    if (_delegate != nil && [_delegate respondsToSelector:@selector(onComment:)]) {
        [_delegate onComment:self.item.content_id];
    }
}
-(void)onShare
{
    [self hideLikeCommentToolbar];
    [self clickShare];
    
}
- (void)clickShare {
    YXCustomActionSheet *cusSheet = [[YXCustomActionSheet alloc] init];
    cusSheet.delegate = self;
    NSArray *contentArray = @[@{@"name":@"朋友圈",@"icon":@"sns_pyq"},
                              @{@"name":@"微信",@"icon":@"sns_wx"},
                              @{@"name":@"新浪微博",@"icon":@"sns_wb"},
                              @{@"name":@"QQ ",@"icon":@"sns_qq"},
                              @{@"name":@"QQ空间 ",@"icon":@"sns_qzone"},];
    
    [cusSheet showInView:[UIApplication sharedApplication].keyWindow contentArray:contentArray];
}

- (void)downloadShareImage {
    
    [[SDWebImageDownloader sharedDownloader]downloadImageWithURL:[NSURL URLWithString:self.item.cover_img] options:SDWebImageDownloaderUseNSURLCache progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
    } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
        self.shareImg = image;
    }];
}

- (void)customActionSheetButtonClick:(YXActionSheetButton *)btn {
    NSLog(@"第%li个按钮被点击了",(long)btn.tag);
    NSInteger index = btn.tag;
    
    DyShareType type;
    switch (index) {
        case 0:
            type = DyShareTypePengYouQuan;
            break;
        case 4:
            type = DyShareTypeQZone;
            break;
        case 2:
            type = DyShareTypeWeiBo;
            break;
        case 1:
            type = DyShareTypeWeiXin;
            break;
        case 3:
            type = DyShareTypeQQ;
            break;
        default:
            break;
    }
    
    [[DyShare getInstance]shareWithShareType:type Content:self.item.title title:self.item.tribe_name url:self.item.shareUrl image:self.shareImg delegate:nil];
    
}
//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC
{
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            NSLog(@"nextResponder:%@",nextResponder);
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}

-(void)onClickUser:(NSString *)userId{
    
    UIViewController *vc = [self getCurrentVC];
    HisHomePageViewController *homepage = [[HisHomePageViewController alloc]init];
    homepage.userid = userId;
    [vc.navigationController pushViewController:homepage animated:YES];
}

-(void)onClickComment:(NSString *)commentId{
    if (_delegate != nil && [_delegate respondsToSelector:@selector(onClickComment:itemId:)]) {
        NSString *itemId = self.item.content_id;
        [_delegate onClickComment:commentId itemId:itemId];
    }
}


- (void)guanzhuAction:(UIButton *)button{
    
    NSLog(@"点击了文章里的关注");
    if (self.guanzhuBtn.selected ==YES) {
        [self loadNetworkQuXiaoGuanZhuData];
    }else{
        [self loadNetworkGuanZhuData];
    }
}
- (void)loadNetworkQuXiaoGuanZhuData{
    
    DYBaseRequest *request = [DYBaseRequest new];
    NSString *follow_obj_id = self.item.user_id;
    NSString *url = [NSString stringWithFormat:@"%@/userCenter/qxAttountByUserId?follow_obj_type=00&follow_obj_id=%@", APIYIGOU,follow_obj_id];
    request.requestUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSLog(@"------%@",request);
        [_guanzhuBtn setBackgroundColor:QINGColor];
        _guanzhuBtn.selected = NO;
        [ViewFactory showSuccess:@"取消关注成功"];
    } error:^(YTKBaseRequest *request) {
        NSData *JSONData = [request.responseString dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
        [ViewFactory showErrorStrMsg:[responseJSON objectForKey:@"message"]];
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
        
    }];
    
}
- (void)loadNetworkGuanZhuData {
    
    DYBaseRequest *request = [DYBaseRequest new];
    
    //    NSDictionary *tmpDic = [self.dataDic objectForKey:@"content"];
    NSString *follow_obj_id = self.item.user_id;
    NSString *url = [NSString stringWithFormat:@"%@/hytribe/setConcern?follow_obj_id=%@", APIYIGOU, follow_obj_id];
    request.requestUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSLog(@"------%@",request);
        [_guanzhuBtn setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
        _guanzhuBtn.selected = YES;
        [ViewFactory showSuccess:@"关注成功"];
    } error:^(YTKBaseRequest *request) {
        NSData *JSONData = [request.responseString dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
        [ViewFactory showErrorStrMsg:[responseJSON objectForKey:@"message"]];
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
        
    }];
}

@end
